<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>认证管理</el-breadcrumb-item>
      <el-breadcrumb-item>真人认证</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片视图区域  -->
    <el-card>
      <el-row :gutter="10">
        <el-col :span="4">
          <!-- 搜索与添加区域 -->
          <el-input
            clearable
            placeholder="请输入名称或电话"
            v-model="queryInfo.sou"
            class="input-with-select">
          </el-input>
        </el-col>
        <el-col :span="2">
          <el-select v-model="queryInfo.type" placeholder="请选择">
            <el-option
              v-for="item in to_examines"
              :key="item.type"
              :label="item.label"
              :value="item.type">
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="getFaceList"
          >搜索
          </el-button>
        </el-col>
      </el-row>
      <!-- 真人认证的列表区域 -->
      <el-table
       :data="faceList"
       border
       v-loading="loading"
       stripe>
        <el-table-column label="编号" type="index"></el-table-column>
        <el-table-column label="用户名称" prop="nickname"></el-table-column>
        <el-table-column label="电话" prop="phone"></el-table-column>
        <el-table-column label="审核凭证" prop="images">
          <template slot-scope="scope">
            <el-image
              style="width: 60px; height: 60px"
              :src="$base.pictureUrl + scope.row.images"
              :preview-src-list="[$base.pictureUrl +  scope.row.images]"
            >
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.to_examine === 0">审核中</el-tag>
            <el-tag type="success" v-else-if="scope.row.to_examine === 1">已认证</el-tag>
            <el-tag type="warning" v-else>已失败</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="提交时间">
          <template slot-scope="scope">
            {{ $dayjs(scope.row.created_at * 1000).format('YYYY-MM-DD HH:mm:ss') }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 审核按钮 -->
            <el-button
              type="primary"
              icon="el-icon-s-check"
              size="small"
              v-if="scope.row.to_examine===0"
              @click="checkUser(scope.row)"
            >审核
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.page"
        :page-sizes="[10, 15, 30, 50]"
        :page-size="queryInfo.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <el-dialog
      title="真人认证审核"
      :visible.sync="checkDialogVisible"
      :close-on-click-modal="false"
      width="30%"
      @close="CheckDialogClosed('checkInfoFormRef')"
    >
      <el-form
        :model="checkInfo"
        :rules="checkInfoFormRules"
        ref="checkInfoFormRef"
        label-width="80px"
      >
        <el-form-item label="用户姓名">
          <!-- disabled禁用 -->
          <el-input v-model="checkInfo.nickname" disabled></el-input>
        </el-form-item>
        <el-form-item label="审核状态">
          <div>
            <el-radio v-model="checkInfo.checkStatus" label="1" border>通过</el-radio>
            <el-radio v-model="checkInfo.checkStatus" label="2" border>拒绝</el-radio>
          </div>
        </el-form-item>
        <el-form-item v-if="checkInfo.checkStatus === '2'" label="拒绝说明" prop="examine_fail_reason">
          <el-input
            type="textarea"
            placeholder="请输入内容"
            v-model="checkInfo.examine_fail_reason"
            maxlength="50"
            show-word-limit
          >
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="checkDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="checkUserFace('checkInfoFormRef')">确 认</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Faces',
  data () {
    return {
      loading: true,
      // 获取认证列表的参数对象
      queryInfo: {
        // 下拉框条件
        type: '0', // 默认审核汇总
        // 搜索的条件
        sou: '',
        // 当前页码
        page: 1,
        // 当前每页显示多少条数据
        size: 10
      },
      to_examines: [
        {
          type: '',
          label: '全部'
        },
        {
          type: '0',
          label: '审核中'
        },
        {
          type: '1',
          label: '已认证'
        },
        {
          type: '2',
          label: '已失败'
        }
      ],
      // 真人数据列表
      faceList: [],
      // 真人数据总条数
      total: 0,
      // 审核弹框的显示与否
      checkDialogVisible: false,
      // 审核信息
      checkInfo: {},
      checkInfoFormRules: {
        // 验证密码是否合法
        examine_fail_reason: [
          {
            required: true,
            message: '请输入拒绝说明',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    this.getFaceList()
  },
  methods: {
    // 审核用户确定
    checkUserFace (formName) {
      this.$refs[formName].validate(async valid => {
        if (!valid) return
        // 通过预效验, 发起请求, 修改用户数据
        const { data: res } = await this.$http.put(
          'admin/faces',
          {
            id: this.checkInfo.id,
            created_by: this.checkInfo.user_id,
            examine_fail_reason: this.checkInfo.examine_fail_reason,
            checkStatus: this.checkInfo.checkStatus
          }
        )
        if (res.code !== 200) return
        // 关闭对话框
        this.checkDialogVisible = false
        // 重新获取用户的列表
        this.getFaceList()
        // 提示修改成功
        this.$message.success('审核用户信息成功!')
      })
    },
    // 监听修改用户对话框的关闭事件
    CheckDialogClosed (formName) {
      this.$refs[formName].resetFields()
    },
    // 审核用户
    checkUser (info) {
      this.checkDialogVisible = true
      this.checkInfo = info
      this.$set(this.checkInfo, 'checkStatus', '1')
      console.log(this.checkInfo)
    },
    // 监听pagesize改变的时间
    handleSizeChange (newSize) {
      this.queryInfo.size = newSize
      this.getFaceList()
    },
    // 监听页码值改变的时间
    handleCurrentChange (newPage) {
      this.queryInfo.page = newPage
      this.getFaceList()
    },
    // 搜索真人认证订单
    async getFaceList () {
      this.loading = true
      const { data: res } = await this.$http.get('admin/faces', { params: this.queryInfo })
      if (res.code !== 200) return
      this.faceList = res.data.list
      this.total = res.data.totalSize
      this.loading = false
    }
  }
}
</script>

<style>
.dialog-footer {
  text-align: center
}

</style>
